<template>
	<view class="">
		<view style="padding-top: 50%;" v-if="list&&!list.length">
			<u-empty></u-empty>
		</view>
		<scroll-view scroll-y v-else>
			<view class="m-5 p-5 bg-white rounded shadow" v-for="i in list">
				<!-- <view class="flex items-center mr-3" style="margin-left: -10px;" @click="i.choosed=!i.choosed"
					v-if="type==1">
					<u-icon name="checkmark-circle" color="#ccc" v-if="!i.choosed"></u-icon>
					<u-icon name="checkmark-circle-fill" color="#2a6757" v-else></u-icon>
				</view> -->
				<view class="flex">
					<view class="mr-3">
						<u-image width="100" height="100" :src="getFilePath(i.goodsSkuCoverPic)" radius="15"></u-image>
					</view>
					<view class="text-sm flex-1">
						<view class="">
							<text class="">SKU名称</text>：
							<text class="text-black-666">{{i.goodsSkuName}}</text>
						</view>
						<view class="">
							<text class="">计量单位</text>：
							<text class="text-black-666">{{i.goodsMeasuringName}}</text>
						</view>
						<view class="flex items-center">
							<view class="">商品数量</view>：
							<view class="text-black-666" v-if="type==0">{{i.goodsCount}}</view>
							<view v-else class="flex items-center text-sm rounded-full"
								style="border: 1px solid #dadbde; height: 25px; padding: 0 5px;">
								<u-icon name="minus" size="12" :color="i.goodsCount==0?'#c8c9cc':'#333'"
									@click="onEditGoodsCount(i,'sub')" v-if="!i.isWeighing"></u-icon>
								<input type="number" v-model="i.goodsCount" class="goods-input">
								<u-icon name="plus" size="12" color="#333" @click="onEditGoodsCount(i,'plus')"
									v-if="!i.isWeighing"></u-icon>
							</view>
						</view>
						<view class="">
							<text class="">商品规格</text>：
							<text class="text-black-666">{{i.goodsSpecification}}</text>
						</view>
						<view class="">
							<text class="">商品总数</text>：
							<text class="text-black-666">{{i.goodsTotal}}</text>
						</view>

						<view class="flex justify-end mt-3" v-if="type==1">
							<view class="">
								<u-button type="error" size="mini" @click="onDelGoods(i)">删除</u-button>
							</view>
							<view class="ml-2">
								<u-button type="primary" size="mini" @click="onUpdateGoods(i)">更新</u-button>
							</view>
						</view>
					</view>
				</view>
				<view class="flex justify-end mt-3" v-if="type==0 && i.goodsStatus != 9">
					<view class="">
						<u-button type="error" size="mini" @click="openSignModal('0',i)">部分拒签</u-button>
					</view>
					<view class="ml-2">
						<u-button type="error" size="mini" @click="onSign('0', i)">拒签</u-button>
					</view>
					<view class="ml-2">
						<u-button type="primary" size="mini" @click="openSignModal('1', i)">部分签收</u-button>
					</view>
					<view class="ml-2">
						<u-button type="primary" size="mini" @click="onSign('1', i)">签收</u-button>
					</view>

				</view>


			</view>
		</scroll-view>
		<u-gap height="64"></u-gap>
		<view class="fixed bottom-0 left-0 right-0 bg-white p-3 u-border-top" v-if="type==1 || type == 3">
			<u-button type="primary" shape="circle" @click="toGoods">添加商品</u-button>
		</view>
		<u-modal :show="showSignModal" :title="'部分'+ modalTitle" showCancelButton confirmColor="#2A6757" @confirm="singModalConfirm" @cancel="showSignModal = false">
			<view class="">
				<u-input type="number" v-model="goodsCount" :placeholder="'请输入'+modalTitle + '数量'"></u-input>
				<!-- <input type="number" v-model="goodsCount" > -->
			</view>
		</u-modal>
	</view>
</template>

<script>
	import request from '@/utils/request.js'
	import {
		getFilePath
	} from '@/utils/index.js'
	import {
		route,
		showModal,
		toast
	} from '../../utils/utils'
	export default {
		data() {
			return {
				showSignModal: false,
				list: null,
				type: 0,
				chooseAll: false,
				modalTitle: '',
				goodsCount: ''
			}
		},
		computed: {
			goodsSum() {
				if (!this.list || !this.list.length) return 0
				console.log(this.list.filter(i => i.choosed))
				return this.list.filter(i => i.choosed).length
			}
		},
		onLoad(query) {
			console.log(query)
			const {
				allocationDocumentId,
				allocationShopsId,
				type,
				shopId,
				purchaseBillId
			} = query

			this.type = type
			this.shopId = shopId
			this.allocationDocumentId = allocationDocumentId
			this.allocationShopsId = allocationShopsId
			this.purchaseBillId = purchaseBillId
		},
		onShow() {
			if (this.type == 1) {
				// 要货单
				this.getDocumentsVoById(this.allocationDocumentId)
				this.getSchainAllocationShops({
					allocationDocumentId: this.allocationDocumentId,
					shopId: this.shopId,
					documentType: 1
				})
			} else if (this.type == 0){
				// 配货单
				this.getShopGoods({
					allocationDocumentId: this.allocationDocumentId,
					allocationShopsId: this.allocationShopsId
				})
			} else if (this.type == 3){
				console.log('我的采货单')
				this.getPurchaseList()
			}
		},
		methods: {
			singModalConfirm() {
				if (!this.goodsCount) return toast(`请输入${this.modalTitle}数量`)
				
				console.log('confirm')
				
				this.showSignModal = false
				const {goodsSpecification, allocationGoodsId} = this.goodsInfo
				console.log(goodsSpecification)
				const weight = this.goodsCount * goodsSpecification
				
				let text, type
				if (this.modalTitle === '签收') {
					text='拒签'
				} else {
					text='签收'
				}
				let content = `该商品${this.modalTitle}${this.goodsCount}份，共计${weight}千克，是否将剩余部分全部${text}`
				showModal({
					content,
					cancelText: '否',
					confirmText: '是'
				}).then(() => {
					console.log()
					if (this.modalTitle === '签收') {
						type= 3
					} else {
						type= 2
					}
					
					this.postSign(allocationGoodsId,type)
				}).catch(() => {
					if (this.modalTitle === '签收') {
						type= 5
					} else {
						type=4
					}
					this.postSign(allocationGoodsId,type)
				})
			},
			openSignModal(type, i) {
				this.goodsInfo = i
				this.showSignModal = true
				if (type == 1) {
					this.modalTitle = '签收'
				} else if (type == 0) {
					this.modalTitle = '拒签'
				}
			},
			onSign(type, i) {
				console.log(type, i)
				const {
					allocationGoodsId
				} = i
				let content
				if (type == 1) {
					content = '确定全部签收么？'
				} else if (type == 0) {
					content = '确定全部拒签么？'
				}
				showModal({
					content
				}).then(() => {
					this.postSign(allocationGoodsId,type)
				})
			},
			postSign(allocationGoodsId,type){
				request('singleSkuSign', {
					method: 'post',
					data: {
						allocationGoodsId,
						type,
						goodsCount: this.goodsCount
					},
					row: true
				}).then(res => {
					if (res.code == 200) {
						toast(res.msg)
						setTimeout(() => {
							this.getDocumentsVoById(this.allocationDocumentId)
							this.getSchainAllocationShops({
								allocationDocumentId: this.allocationDocumentId,
								shopId: this.shopId,
								documentType: 1
							})
						}, 1000)
					} else {
						toast(res.msg)
					}
				})
			},
			toGoods() {
				console.log(this.list)
				const goodsSkuIdList = this.list.map(i => i.goodsSkuId)
				console.log(goodsSkuIdList)
				route('/pages/goods/index', {
					allocationShopsId: this._allocationShopsId,
					allocationDocumentId: this._allocationDocumentId,
					goodsSkuIdList,
					type: this.type,
					purchaseBillId: this.purchaseBillId
				})
			},
			onDelGoods(i) {
				showModal({
					content: '确定删除该商品么？'
				}).then(() => {
					request(`allocationShopGoods/${i.allocationGoodsId}`, {
						method: 'delete',
						row: true
					}).then(res => {
						if (res.code == 200) {
							toast('删除成功')
							setTimeout(() => {
								this.getSchainAllocationShops({
									allocationDocumentId: this.allocationDocumentId,
									shopId: this.shopId,
									documentType: 1
								})
							}, 1000)
						} else {
							toast(res.msg)
						}
					})
				})

			},
			onAddBatch() {
				if (!this.goodsSum) return toast('请先选择商品')

				const data = this.list.filter(i => i.choosed).map(i => {
					return {
						allocationDocumentId: i.allocationDocumentId,
						allocationShopsId: i.allocationShopsId,
						goodsCount: 0,
						goodsTotal: 0,
						goodsId: i.goodsId,
						goodsSkuId: i.goodsSkuId,
						goodsMeasuringUnitId: i.goodsMeasuringUnitId,
						goodsMeasuringName: i.goodsMeasuringName,
						goodsSpecification: i.goodsSpecification
					}
				})

				request('addAllocationShopGoodsBatch', {
					method: 'post',
					data,
					row: true
				}).then(res => {
					console.log(res)
					if (res.code == 200) {
						toast('添加成功')
						setTimeout(() => {
							this.getSchainAllocationShops({
								allocationDocumentId: this.allocationDocumentId,
								shopId: this.shopId,
								documentType: 1
							})
						}, 1000)
					} else {
						toast(res.msg)
					}
				})
			},
			onUpdateGoods(i) {
				if (!i.choosed) return toast('请先修改商品数量')
				showModal({
					content: `确定修改当前商品数量为${i.goodsCount}吗？`
				}).then(res => {
					const {
						allocationGoodsId,
						goodsCount
					} = i
					const data = {
						allocationGoodsId,
						goodsCount
					}
					request('allocationShopGoods', {
						method: 'put',
						data,
						row: true
					}).then(res => {
						console.log(res)
						if (res.code == 200) {
							toast('更新成功')
							setTimeout(() => {
								this.getShopGoods({
									allocationShopsId: this._allocationShopsId,
									allocationDocumentId: this._allocationDocumentId
								})
							}, 1000)
						} else {
							toast(res.msg)
						}
					})
				})
			},
			onEditGoodsCount(i, type) {
				console.log(i, type)
				i.choosed = true
				if (type == 'sub') {
					if (i.goodsCount == 0) {
						console.log('删除该商品')
						return
					} else {
						i.goodsCount--
					}

				} else if (type == 'plus') {
					console.log('plus')
					i.goodsCount++
				}
			},
			toggleChooseAll() {
				this.chooseAll = !this.chooseAll
				this.list.forEach(i => i.choosed = this.chooseAll)
			},
			getFilePath,
			getSchainAllocationShops(data) {
				request('schainAllocationShops', {
					data,
					row: true
				}).then(res => {
					console.log(res)
					if (res.code === 200) {
						if (res.rows.length !== 1) return toast('数据异常请联系管理人员')
						const {
							allocationShopsId,
							allocationDocumentId
						} = res.rows[0]
						this._allocationShopsId = allocationShopsId
						this._allocationDocumentId = allocationDocumentId
						this.getShopGoods({
							allocationShopsId,
							allocationDocumentId
						})
					}
				})
			},
			getPurchaseList() {
				request('purchaseGoods', {
					data: {
						purchaseBillId: this.purchaseBillId,
						isFinish:1
					},
					row: true
				}).then(res => {
					console.log(res)
					
					if (res.code === 200) {
						this.list = res.rows
					}
				})
			},
			getShopGoods(data) {
				request('schainAllocationShopGoods', {
					data,
					row: true
				}).then(res => {
					console.log(res)
					if (res.code === 200) {
						res.rows.forEach(i => i.choosed = false)
						this.list = res.rows
					}
				})
			},
			getDocumentsVoById(id) {
				request(`selectAllocationDocumentsVoById/${id}`).then(res => {
					console.log(res)
					// if (res.code === 200) {
					// 	this.list = res.rows
					// }
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.goods-input {
		font-size: 14px;
		width: 25px;
		text-align: center;
		color: #999;
	}
</style>
